/*
 * Copyright (c) 2006 Sun Microsystems, Inc.  All rights reserved.  U.S.
 * Government Rights - Commercial software.  Government users are subject
 * to the Sun Microsystems, Inc. standard license agreement and
 * applicable provisions of the FAR and its supplements.  Use is subject
 * to license terms.
 *
 * This distribution may include materials developed by third parties.
 * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
 * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
 * other countries.
 *
 * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits reserves.
 *
 * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
 * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
 * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
 * en vigueur de la FAR (Federal Acquisition Regulations) et des
 * supplements a celles-ci.  Distribue par des licences qui en
 * restreignent l'utilisation.
 *
 * Cette distribution peut comprendre des composants developpes par des
 * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
 * sont des marques de fabrique ou des marques deposees de Sun
 * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
 */


package database;

import java.sql.*;
import java.util.ArrayList;

import BCrypt.BCrypt;

//import exception.*;
//import cart.*;

// The instance of BookDBAO gets created when the application
// is deployed. It maintains the Connection object to the
// database. The Connection object is created from DataSource
// object, which is retrieved through JNDI.
// For more information on DataSource, please see
// http://java.sun.com/j2se/1.4.2/docs/api/javax/sql/DataSource.html.
public class DietPlanDBAO extends DietPlan{

    Connection con;
    private boolean conFree = true;
    
    
    public DietPlanDBAO() throws Exception {
        try {
        	
        	DB db = new DB();
        	con = db.getConnection();
            
        } catch (Exception ex) {
            System.out.println("Exception in BookDBAO: " + ex);
            throw new Exception("Couldn't open connection to database: " +
                    ex.getMessage());
        }
    }
    
    public void remove() {
        try {
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
    
    protected synchronized Connection getConnection() {
        while (conFree == false) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        
        conFree = false;
        notify();
        
        return con;
    }
    
    protected synchronized void releaseConnection() {
        while (conFree == true) {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        
        conFree = true;
        notify();
    }
    
    public boolean createDietPlan(String username, int dietType, java.util.Date dietStart, double startWeight, double goalWeight, java.util.Date goalDate, int dailyCalTarget) {
    	boolean success = false;
        try {
            String selectStatement = "INSERT INTO dietplan(username,firstName,lastName,city,zipCode,country) VALUES(?,?,?,?,?,?)";
            getConnection();
            
            PreparedStatement prepStmt = con.prepareStatement(selectStatement);
            prepStmt.setString(1, username);
            prepStmt.setInt(2, dietType);
            java.sql.Date sqlDate = new java.sql.Date(dietStart.getTime());
            prepStmt.setDate(3, sqlDate);
            prepStmt.setDouble(4, startWeight);
            prepStmt.setDouble(5, goalWeight);
            java.sql.Date sqlDate1 = new java.sql.Date(goalDate.getTime());
            prepStmt.setDate(6, sqlDate1);
            prepStmt.setInt(7, dailyCalTarget);
            
            if (prepStmt.executeUpdate()==1) {
            	success = true;
            } 
            
            prepStmt.close();
            releaseConnection();
           
        } catch (SQLException ex) {
            releaseConnection();
            ex.printStackTrace();
        }
        return success;
    }
    
    public boolean getDietPlan() {
    	boolean success = false;
        try {
            String selectStatement = "select * from dietplan where username = ?";
            getConnection();
            
            PreparedStatement prepStmt = con.prepareStatement(selectStatement);
            prepStmt.setString(1, username);
            
            System.out.println(prepStmt.toString());
            ResultSet rs = prepStmt.executeQuery();

            
            while (rs.next()) {
            	//setUsername(rs.getString("username"));
            	setDietType(rs.getInt("dietType"));
            	
            	java.sql.Date sqlDate = (rs.getDate("dietStart"));
            	java.util.Date utilDate = new java.util.Date(sqlDate.getTime());
            	setDietStart(utilDate);
            	
            	setStartWeight(rs.getDouble("startWeight"));
            	setGoalWeight(rs.getDouble("goalWeight"));
            	
            	java.sql.Date sqlDate1 = (rs.getDate("goalDate"));
            	java.util.Date utilDate1 = new java.util.Date(sqlDate1.getTime());
            	setGoalDate(utilDate1);
            	
            	setDailyCalTarget(rs.getInt("dailyCalTarget"));
            	
            	success = true;
            } 
            
            prepStmt.close();
            releaseConnection();
           
        } catch (SQLException ex) {
            releaseConnection();
            ex.printStackTrace();
        }
        return success;
    }
    
    public boolean updateDietPlan() {
    	boolean success = false;
        try {
            String selectStatement = "UPDATE dietplan SET dietType=?, dietStart=?, startWeight=?, goalWeight=?, goalDate=?, dailyCalTarget=? WHERE username = ?";
            getConnection();
            
            PreparedStatement prepStmt = con.prepareStatement(selectStatement);
            prepStmt.setInt(1, dietType);
            
            java.sql.Date sqlDate = new java.sql.Date(dietStart.getTime());
            prepStmt.setDate(2, sqlDate);
            
            
            prepStmt.setDouble(3, startWeight);
            
            prepStmt.setDouble(4, goalWeight);
            
            java.sql.Date sqlDate1 = new java.sql.Date(goalDate.getTime());
            prepStmt.setDate(5, sqlDate1);
            
            prepStmt.setDouble(6, dailyCalTarget);
            prepStmt.setString(7, username);
            
            if (prepStmt.executeUpdate()==1) {
            	success = true;
            } 
            
            prepStmt.close();
            releaseConnection();
           
        } catch (SQLException ex) {
            releaseConnection();
            ex.printStackTrace();
        }
        return success;
    }
    
    

    public static void main (String [] args) throws Exception
    {
    	AboutInfoDBAO ai = new AboutInfoDBAO();
    	System.out.println(ai.createAboutInfo("111","111", "111", "111", "111", "111"));
    }

}
